package pure.csw.activemq.topicFeatureTest;

import org.apache.activemq.ActiveMQConnectionFactory;
import pure.csw.common.Constant;

import javax.jms.*;

public class TopicPublisher {
    public static void main(String[] args) throws JMSException, InterruptedException {
        //注册工厂，开启连接
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(Constant.URL);
        Connection connection = factory.createConnection();
        connection.start();
        //获取一次会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建Topic和Publisher
        Topic topic = session.createTopic(Constant.TOPICNAME);
        MessageProducer producer = session.createProducer(topic);
        TextMessage textMessage = session.createTextMessage();
        System.out.println("subscriber未启动发送：在subscriber未启动时发送");
        textMessage.setText("在subscriber未启动时发送");
        producer.send(textMessage);
        //休眠10s后在发送
        System.out.println("10s中等待subscriber启动");
        Thread.sleep(10000);
        System.out.println("再次发送：在subscriber启动后发送");
        textMessage.setText("在subscriber启动后发送");
        producer.send(textMessage);
        //等待资源发送完毕
        Thread.sleep(800);
        producer.close();
        session.close();
        connection.close();
    }
}
